package common.action;

import hotel.service.IHotelOrderService;
import hotel.service.IHotelReviewService;
import java.io.IOException;
import java.net.InetAddress;
import java.sql.Timestamp;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import system.service.IAdminService;
import common.pojo.SysAdmin;
import static common.utils.Constants.*;
import common.utils.MD5Util;
import common.utils.Page;
import common.utils.PageUtil;
import common.utils.PaginationUtil;

@Scope("prototype")
@Controller(value = "admin")
public class AdminAction extends BaseAction {

	/**
	 * 登录验证
	 * 
	 * @throws IOException
	 */
	public void login() throws IOException {
		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		SysAdmin ad = adminService.getByName(name);
		if (ad == null) {
			response.getWriter().write("{\"success\":false,\"msg\":1}");
		} else if (!MD5Util.md5(pwd).equals(ad.getPassword())) {
			response.getWriter().write("{\"success\":false,\"msg\":2}");
		} else {
			response.getWriter().write("{\"success\":true}");
			session.setAttribute(ADMIN_ID, ad.getId());
			session.setAttribute(LOGINNAME, name);
			session.setAttribute(PERMIT, ad.getPermit());
			Timestamp d = new Timestamp(System.currentTimeMillis());
			ad.setLastLoginTime(d);
			InetAddress addr = InetAddress.getLocalHost();
			String ip = addr.getHostAddress().toString();
			ad.setLastLoginIp(ip);
			adminService.update(ad);
		}

	}
	/**
	 * 未审核的订单和点评数
	 * @throws Exception
	 */
	public void getOrderAndReview() throws Exception{
		 ordercount =hotelOrderService.getCount("  verify=0 and tradeStatus!=5 ");
		 reviewcount=reviewService.getCount(" where status=0 ");
		 response.getWriter().write("{\"ordercount\":"+ordercount+",\"reviewcount\":"+reviewcount+"}");
	}
	
	/**
	 * 获取列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getAdminListByPage() throws Exception {
		String currentTmp = request.getParameter("current");
		int total= adminService.getCount();
		int current = PageUtil.getCurrent(currentTmp);
		Page page = PageUtil.createPage(LIMIT, total, current);
		adminlist = adminService.find(page.getBeginIndex(), LIMIT);
		pagination = PaginationUtil.getPaginationStyleTwo(page,
				"getAdminListByPage.action");
		return "adminList";
	}

	/**
	 * 修改密码
	 * 
	 * @return
	 * @throws IOException
	 */
	public String updatepwd() throws IOException {
		String oldPWD = request.getParameter("oldpwd");
		String newPWD = request.getParameter("newpwd");
		SysAdmin ad = adminService.findById((Integer) session
				.getAttribute(ADMIN_ID));
		if (MD5Util.md5(oldPWD).equalsIgnoreCase(ad.getPassword())) {
			ad.setPassword(MD5Util.md5(newPWD));
			adminService.updatepwd(ad);
			return "success2";
		} else {
			return "exception";
		}
	}

	public String findById() {
		int id = Integer.valueOf(request.getParameter("id"));
		admin = adminService.findById(id);
		return "detail";
	}


	public String saveAdmin() {
		adminService.saveAdmin(admin);
		if (StringUtils.isBlank(dofunction))
			dofunction = "()";// 刷新页面
		return "success2";
	}

	public void checkAddmin() throws Exception{
		String name=request.getParameter("loginName");
		admin=adminService.getByName(name);
		if(admin!=null){
			response.getWriter().write("{\"success\":false}");
		}else{
			response.getWriter().write("{\"success\":true}");
		}		
	}
	
	public String updateAdmin() {
		adminService.updateAdmin(admin);
		if (StringUtils.isBlank(dofunction))
			dofunction = "()";// 刷新页面
		return "success2";
	}

	public String deleteById() {
		String ids = request.getParameter("ids");
		adminService.delete(ids);
		return SUCCESS;
	}

	public String destorySession() {
		session.invalidate();
		return "index";
	}

	private static final long serialVersionUID = 1L;

	@Autowired
	private IAdminService adminService;
	@Autowired
	private IHotelOrderService hotelOrderService;
	@Resource
	private IHotelReviewService reviewService;
	private int ordercount;
	private int reviewcount;
	private SysAdmin admin;
	private List<SysAdmin> adminlist;
	private String pagination;
	private String dofunction;

	public int getOrdercount() {
		return ordercount;
	}

	public void setOrdercount(int ordercount) {
		this.ordercount = ordercount;
	}

	public int getReviewcount() {
		return reviewcount;
	}

	public void setReviewcount(int reviewcount) {
		this.reviewcount = reviewcount;
	}



	public String getDofunction() {
		return dofunction;
	}

	public void setDofunction(String dofunction) {
		this.dofunction = dofunction;
	}

	public String getPagination() {
		return pagination;
	}

	public void setPagination(String pagination) {
		this.pagination = pagination;
	}

	public List<SysAdmin> getAdminlist() {
		return adminlist;
	}

	public void setAdminlist(List<SysAdmin> adminlist) {
		this.adminlist = adminlist;
	}

	public SysAdmin getAdmin() {
		return admin;
	}

	public void setAdmin(SysAdmin admin) {
		this.admin = admin;
	}

}
